let btn = document.getElementById("btn");

btn.onclick = function () {
    let xhr = null;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhr.open("post", "/ajax", true); // 第三个参数指明是否为异步

    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // 只在请求为post时才需要声明

    xhr.send();

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                console.log(xhr.responseText);
            }
        }
    }
}